import { createRouter, createWebHashHistory } from "vue-router";
import Layout from "@/pages/layout/index.vue";

const routes = [
  {
    path: "/login",
    name: "login",
    meta: {
      title: "登录",
    },
    component: () => import("@/pages/login.vue"),
  },
  {
    path: "/",
    component: Layout,
    redirect: "home",
    children: [
      {
        path: "/home",
        name: "home",
        meta: {
          title: "首页",
        },
        component: () => import("@/pages/homePage/index.vue"),
      },
      {
        path: "/user",
        name: "user",
        meta: {
          title: "个人中心",
        },
        component: () => import("@/pages/homePage/user.vue"),
      },
      {
        path: "/feedback",
        name: "feedback",
        meta: {
          title: "系统反馈",
        },
        component: () => import("@/pages/homePage/feedback.vue"),
      },
      {
        path: "/addFeedback",
        name: "addFeedback",
        meta: {
          title: "新增系统反馈",
        },
        component: () => import("@/pages/homePage/addFeedback.vue"),
      },
      {
        path: "/news",
        name: "news",
        meta: {
          title: "个性化推荐",
        },
        component: () => import("@/pages/homePage/news.vue"),
      },
      {
        path: "/newsDetail",
        name: "newsDetail",
        meta: {
          title: "新闻详情",
        },
        component: () => import("@/pages/homePage/newsDetail.vue"),
      },
    ],
  },
];

const router = createRouter({
  history: createWebHashHistory(),
  routes: routes,
});

router.beforeEach((to, from, next) => {
  if (!localStorage.getItem("token") && to.path !== "/login") {
    next("/login");
  } else {
    next();
  }
});
export default router;
